package com.example.yrdev.ordering_system.Common;

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;

import com.example.yrdev.ordering_system.Utils.GlobalFunction;
import com.example.yrdev.ordering_system.Utils.MIReaderUtils;

import org.json.JSONObject;

/**
 * Created by luke on 11/24/16.
 */

public class Setting {
 public static String[] reportModeList= {"定额","随机","商品"};
    public static   final String DH_TABLE_NAME = "SystemSetting";

    public static   final String DH_devID="devID";//商品编号
    public static   final String DH_security="security";//商品名称
    public static  final String DH_minimalMoney="minimalMoney";//数量
    public static  final String DH_iCCardDataCylinder="DH_iCCardDataCylinder";//数量
    public static  final String DH_DesCode="DesCode";//数量
    public static final  String DH_SellerType="SellerType";
    public static final  String DH_maxPay="maxPay";
    public static final  String DH_chargeBalance="chargeBalance";
    public static final  String DH_regiscode="regiscode";
    public static final  String DH_shopName="shopName";

    //   public static  final String item_discountable="tem_discountable";

    public  static final String DH_CREATE_TABLE="CREATE TABLE " + DH_TABLE_NAME + "( " + DH_devID + " TEXT," + DH_security + " TEXT,"
            + DH_minimalMoney + " Double," + DH_iCCardDataCylinder + " TEXT," +  DH_regiscode+"TEXT"+")";


    private String devID="1";

    private  String serviceTip;
    private String security;
    private String passwordFlag;
    private String iCCardDataCylinder;
    private String minimalMoney;
    private String  regiscode;
    private static Setting instance;
    private Context context;
    private boolean printMode;
    private String DesCode;
    private int sellerType;
    private String maxPay;
    private float chargeBalance;

    public String shopName;

    private final String devIdKey="devIdKey";

    public static Setting getInstance(){
        if (instance==null){
            instance=new Setting();
            instance.init();

        }
        return instance;
    }

    public int getWritingSector(){

      return Integer.valueOf(MIReaderUtils.convertToDecimal1(iCCardDataCylinder));
        //MIReaderUtils.convertToDecimal1()
    }




    public void parseJSON(JSONObject object, Context context){

        try {
            if (this.context==null){

            this.context=context;


        }


            instance.setiCCardDataCylinder(object.getString("iCCardDataCylinder"));

            instance.setSecurity(object.getString("security"));

            String temp=object.getString("minimalMoney");

            if (temp.length()>0){

             instance.setMinimalMoney("0.0");

            }

            instance.sellerType=object.getInt("sellerType");

            instance.maxPay=object.getString("maxPay");
            instance.shopName=object.getString("shopName");

            saveSetting();




        }catch (Exception e){

            GlobalFunction.printTextByTag("parse error");
        }


    }
    public String getDevID() {
        return devID;
    }

    public String getSecurity() {
        return security;
    }

    public String getMinimalMoney() {
        return minimalMoney;
    }

    public String getiCCardDataCylinder() {
        return iCCardDataCylinder;
    }

    public boolean getPrintMode(){
        return this.printMode;

    }
    public String getRegiscode() {        return regiscode;    }
    public void setRegiscode(String  regiscode){
        this.regiscode=regiscode;
    }
    public void setChargeBalance(float chargeBalance) {
        this.chargeBalance = chargeBalance;
        saveSetting();
    }


    public void reduceChargeBalance(double chargeBalance) {
        this.chargeBalance=this.chargeBalance-(float)chargeBalance;
        GlobalFunction.printTextByTag("reduce:"+this.chargeBalance);
        setChargeBalance(this.chargeBalance);
    }

    public float getChargeBalance() {
        return chargeBalance;
    }

    public void setDevID(String devID) {
        this.devID = devID;
    }

   private   void setSecurity(String security) {
        this.security = security;
    }

    public void setMinimalMoney(String minimalMoney) {
        this.minimalMoney = minimalMoney;
    }

    private   void setiCCardDataCylinder(String iCCardDataCylinder) {
        this.iCCardDataCylinder = iCCardDataCylinder;
    }

    public void setDesCode(String desCode) {
        DesCode = desCode;
    }

    public String getDesCode() {
        return DesCode;
    }


   public void init(){


        SharedPreferences preferences= GlobalFunction.context.getSharedPreferences("setting",Context.MODE_PRIVATE);

        devID=preferences.getString(DH_devID,"1");
        minimalMoney=preferences.getString(DH_minimalMoney,"");
        security=preferences.getString(DH_security,"");
        maxPay=preferences.getString(DH_maxPay,"");
        DesCode=preferences.getString(DH_DesCode,"");
        sellerType=preferences.getInt(DH_SellerType,0);
        chargeBalance=preferences.getFloat (DH_chargeBalance,.0f);
        regiscode= preferences.getString(DH_regiscode,"");
        shopName=preferences.getString(DH_shopName,"");

       GlobalFunction.printTextByTag("type"+sellerType+":"+security);
    }

    private  int printStat;

    public void assignPrintingMode(int stat){
        printStat=stat;

        boolean printMode;
        if (stat==0){
            printMode=false;

        }else {
            printMode=true;

        }

        saveSetting();
    }

    public String getDeviceID(){

        //return   "218f809ab19a333f";

         return       Settings.Secure.getString(GlobalFunction.context.getContentResolver(),     Settings.Secure.ANDROID_ID);


    }

    public void  saveSetting(){

        SharedPreferences preferences= GlobalFunction.context.getSharedPreferences("setting",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putString(DH_iCCardDataCylinder,iCCardDataCylinder);
        editor.putString(DH_security,security);
        editor.putString(DH_devID,devID);
        editor.putString(DH_minimalMoney,minimalMoney);
        editor.putString(DH_maxPay,maxPay);
        editor.putString(DH_DesCode,getDesCode());
        editor.putInt(DH_SellerType,sellerType);
        editor.putFloat(DH_chargeBalance,chargeBalance) ;
        editor.putString(DH_regiscode,regiscode);
        editor.putString(DH_shopName,shopName);

        editor.apply();

        GlobalFunction.printTextByTag("security"+security);




    }


    public  void saveDevID(){

        SharedPreferences preferences= GlobalFunction.context.getSharedPreferences("setting",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putString(DH_devID,devID).apply();


    }

    public  void saveDevID(String devID){

        this.devID=devID;
        SharedPreferences preferences= GlobalFunction.context.getSharedPreferences("setting",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putString(DH_devID,devID).apply();


    }


    public void setSellerType(int sellerType) {
        this.sellerType = sellerType;
        saveSetting();

    }



    public int getSellerType() {
        return sellerType;
    }

    public Boolean checkWorkingMode(int mode){
        boolean stat;
        if (mode==sellerType)

           stat=true;

        else {
            stat=false;
            GlobalFunction.ToastAlert(context,"你正在使用"+ Setting.reportModeList[sellerType]+"工作模式");
         //   GlobalFunction.printTextByTag();
        }


        return stat;

    }
}
